package com.isay.app.honey.ui.cash;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.isay.app.honey.R;
import com.isay.app.honey.manager.NetworkManager;
import com.isay.frameworklib.okhttp.ICommonCallback;
import com.isay.frameworklib.ui.BaseActivity;
import com.isay.frameworklib.utils.ToastUtils;

import org.json.JSONObject;

/**
 * 提现
 */
public class GoCashActivity extends BaseActivity implements View.OnClickListener {


    private TextView mTvCanMoney;
    private EditText mEdtInMoney;
    private View mCashSuccess;

    public static void launch(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, GoCashActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreateActivity(Bundle savedInstanceState) {
        setContentView(R.layout.activity_cash_go);
        initView();
        initData();
    }

    @Override
    protected int getStatusBarColor() {
        return R.color.white;
    }


    private void initData() {
        getCanCash();
    }

    private void initView() {
        ((TextView) findViewById(R.id.tv_title)).setText("提现");
        findViewById(R.id.iv_go_back).setOnClickListener(this);
        findViewById(R.id.tv_cash_commit).setOnClickListener(this);
        mTvCanMoney = findViewById(R.id.tv_can_money);
        mEdtInMoney = findViewById(R.id.edt_in_money);
        mCashSuccess = findViewById(R.id.lay_cash_success);
        mCashSuccess.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_go_back:
                finish();
                break;
            case R.id.tv_cash_commit:
                getCash();
                break;
            default:
                break;
        }
    }


    private void getCanCash() {
        NetworkManager.getCanCash(new ICommonCallback() {
            @Override
            public void onSuccess(int ret, String msg, String content) {
                try {
                    JSONObject jsonObject = new JSONObject(content);
                    String money = jsonObject.optString("money");
                    mTvCanMoney.setText("可提现金额" + money);
                } catch (Exception e) {
                    onFailure(e);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }


    private void getCash() {
        NetworkManager.getCash(mEdtInMoney.getText().toString().trim(), new ICommonCallback() {
            @Override
            public void onSuccess(int ret, String msg, String content) {
                if (ret == 200) {
                    mCashSuccess.setVisibility(View.VISIBLE);
                } else {
                    ToastUtils.show(msg);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }

}
